package top.cptr.ipmitooljavarpc.service;

import cn.hutool.core.util.RuntimeUtil;
import cn.hutool.core.util.StrUtil;
import top.cptr.ipmitooljavarpc.common.CommonErrorReturn;
import top.cptr.ipmitooljavarpc.common.Constants;
import top.cptr.ipmitooljavarpc.common.R;
import top.cptr.ipmitooljavarpc.domain.dto.BaseDto;
import top.cptr.ipmitooljavarpc.domain.dto.SetFanSpeedDto;


/**
 * @author Tiger_L
 * @version 1.0
 * @description 风扇控制
 * @date 2024/9/16 15:14
 */
public class FanControl {


    /**
     * 设置风扇为自动
     *
     * @param params 参数
     * @return 设置结果
     */
    public R<Object> fanAuto(BaseDto params) {
        String command = Constants.GLOBAL_COMMAND + "raw 0x30 0x30 0x01 0x01";
        String format = StrUtil.format(command, params.getHost(), params.getUser(), params.getPassword());
        String re = RuntimeUtil.execForStr(format);
        if (!CommonErrorReturn.check(re)) {
            return R.fail(re);
        }
        return R.ok();
    }


    /**
     * 设置风扇转速百分比
     *
     * @param params 参数
     * @return 结果
     */
    public R<Object> setFanSpeed(SetFanSpeedDto params) {
//        关闭自动调速
        String command = Constants.GLOBAL_COMMAND + "raw 0x30 0x30 0x01 0x00";
        String format = StrUtil.format(command, params.getHost(), params.getUser(), params.getPassword());
        String re = RuntimeUtil.execForStr(format);
        if (!CommonErrorReturn.check(re)) {
            return R.fail(re);
        }
//        设置自动调速
        command = Constants.GLOBAL_COMMAND + "raw 0x30 0x30 0x02 0xff 0x" + Integer.toHexString(params.getPercentage());
        format = StrUtil.format(command, params.getHost(), params.getUser(), params.getPassword());
        re = RuntimeUtil.execForStr(format);
        if (!CommonErrorReturn.check(re)) {
            return R.fail(re);
        }
        return R.ok();
    }
}
